﻿using System;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Collections.Generic;
using System.Web.Routing;

namespace Helpers
{
    public static class ValidationExtensions
    {
        /// <summary>
        /// Renders an HTML form ValidationMessage
        /// </summary>
        public static MvcHtmlString MyValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression)
        {
             string elementName = ExpressionHelper.GetExpressionText(expression);
             MvcHtmlString normal = html.ValidationMessageFor(expression);
             if (normal != null)
             {
                 string regexp = "<span class=\"field-validation-error\">(?<message>.*?)</span>";
                 Match tableMatch = Regex.Match(normal.ToHtmlString(), regexp);
                 string message = tableMatch.Groups[0].ToString();

                 string newValidator = String.Format("<div class=\"error-left\"></div><div class=\"error-inner\">{0}</div>", message);
 
                 return MvcHtmlString.Create(newValidator);
             }
             return null;
        }

        public static MvcHtmlString MyCheckBox(this HtmlHelper html, String Id, Boolean Checked)
        {
            if (Checked)
            {
                return MvcHtmlString.Create(String.Format("<input type='checkbox' id='{0}' name='{1}' checked='checked'/>", Id, Id));
            }
            else
            {
                return MvcHtmlString.Create(String.Format("<input type='checkbox' id='{0}' name='{1}'/>", Id, Id));
            }
        }

        /// <summary>
        /// Renders an HTML form TextBox
        /// </summary>
        public static MvcHtmlString MyTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            MvcHtmlString errorHTML = htmlHelper.ValidationMessageFor(expression);
            if (errorHTML == null)
            {
                return htmlHelper.TextBoxFor(expression, new { @class = "inp-form" });
            }
            else
            {
                return MvcHtmlString.Create(htmlHelper.TextBoxFor(expression, new { @class = "inp-form-error" }).ToHtmlString().Replace("input-validation-error ", ""));
            }
        }

        /// <summary>
        /// Renders an HTML form TextBox
        /// </summary>
        public static MvcHtmlString MyTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, Expression<Func<TModel, TProperty>> CheckErrorExpression)
        {
            MvcHtmlString errorHTML = htmlHelper.ValidationMessageFor(CheckErrorExpression);
            if (errorHTML == null)
            {
                return htmlHelper.TextBoxFor(expression, new { @class = "inp-form" });
            }
            else
            {
                return MvcHtmlString.Create(htmlHelper.TextBoxFor(expression, new { @class = "inp-form-error" }).ToHtmlString().Replace("input-validation-error ", ""));
            }
        }

        /// <summary>
        /// Renders an HTML form TextBox
        /// </summary>
        public static MvcHtmlString MyPasswordFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            MvcHtmlString errorHTML = htmlHelper.ValidationMessageFor(expression);
            if (errorHTML == null)
            {
                return htmlHelper.PasswordFor(expression, new { @class = "inp-form" });
            }
            else
            {
                return MvcHtmlString.Create(htmlHelper.PasswordFor(expression, new { @class = "inp-form-error" }).ToHtmlString().Replace("input-validation-error ", ""));
            }
        }

        /// <summary>
        /// Renders an HTML form TextArea
        /// </summary>
        public static MvcHtmlString MyTextAreaFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            MvcHtmlString errorHTML = htmlHelper.ValidationMessageFor(expression);
            if (errorHTML == null)
            {
                return htmlHelper.TextAreaFor(expression, new { @class = "form-textarea" });
            }
            else
            {
                return htmlHelper.TextAreaFor(expression, new { @class = "form-textarea-error" });
            }
        }

        /// <summary>
        /// Renders an HTML form TextBox
        /// </summary>
        public static MvcHtmlString ProjectLookupDropDownList<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, String Id, Expression<Func<TModel, TProperty>> CheckErrorExpression)
        {
            MvcHtmlString errorHTML = htmlHelper.ValidationMessageFor(CheckErrorExpression);
            if (errorHTML == null)
            {
                return MvcHtmlString.Create(String.Format(@"<select id='{0}' name='{1}' class='styledselect_form_1'>
                                                                <option value=''>Select {2}</option>
                                                            </select>", Id, Id, Id));
            }
            else
            {
                return MvcHtmlString.Create(String.Format(@"<select id='{0}' name='{1}' class='styledselect_form_1_error'>
                                                                <option value=''>Select {2}</option>
                                                            </select>", Id, Id, Id));
            }
        }
    }
}